home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / vm / vm-misc.el.z / vm-misc.el
Encoding:
Text File  |  1998-05-21  |  24.2 KB  |  774 lines

  1. ;;; Miscellaneous functions for VM
  2. ;;; Copyright (C) 1989-1997 Kyle E. Jones
  3. ;;;
  4. ;;; This program is free software; you can redistribute it and/or modify
  5. ;;; it under the terms of the GNU General Public License as published by
  6. ;;; the Free Software Foundation; either version 1, or (at your option)
  7. ;;; any later version.
  8. ;;;
  9. ;;; This program is distributed in the hope that it will be useful,
  10. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. ;;; GNU General Public License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU General Public License
  15. ;;; along with this program; if not, write to the Free Software
  16. ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. (provide 'vm-misc)
  19.  
  20. (defun vm-delete-non-matching-strings (regexp list &optional destructively)
  21.   "Delete strings matching REGEXP from LIST.
  22. Optional third arg non-nil means to destructively alter LIST, instead of
  23. working on a copy.
  24.  
  25. The new version of the list, minus the deleted strings, is returned."
  26.   (or destructively (setq list (copy-sequence list)))
  27.   (let ((curr list) (prev nil))
  28.     (while curr
  29.       (if (string-match regexp (car curr))
  30.       (setq prev curr
  31.         curr (cdr curr))
  32.     (if (null prev)
  33.         (setq list (cdr list)
  34.           curr list)
  35.       (setcdr prev (cdr curr))
  36.       (setq curr (cdr curr)))))
  37.     list ))
  38.  
  39. (defun vm-parse (string regexp &optional matchn)
  40.   (or matchn (setq matchn 1))
  41.   (let (list)
  42.     (store-match-data nil)
  43.     (while (string-match regexp string (match-end 0))
  44.       (setq list (cons (substring string (match-beginning matchn)
  45.                   (match-end matchn)) list)))
  46.     (nreverse list)))
  47.  
  48. (defun vm-parse-addresses (string)
  49.   (if (null string)
  50.       ()
  51.     (let (work-buffer)
  52.       (save-excursion
  53.        (unwind-protect
  54.        (let (list start s char)
  55.          (setq work-buffer (generate-new-buffer "*vm-work*"))
  56.          (set-buffer work-buffer)
  57.          (insert string)
  58.          (goto-char (point-min))
  59.          (skip-chars-forward "\t\f\n\r ")
  60.          (setq start (point))
  61.          (while (not (eobp))
  62.            (skip-chars-forward "^\"\\,(")
  63.            (setq char (following-char))
  64.            (cond ((= char ?\\)
  65.               (forward-char 1)
  66.               (if (not (eobp))
  67.               (forward-char 1)))
  68.              ((= char ?,)
  69.               (setq s (buffer-substring start (point)))
  70.               (if (or (null (string-match "^[\t\f\n\r ]+$" s))
  71.                   (not (string= s "")))
  72.               (setq list (cons s list)))
  73.               (skip-chars-forward ",\t\f\n\r ")
  74.               (setq start (point)))
  75.              ((= char ?\")
  76.               (re-search-forward "[^\\]\"" nil 0))
  77.              ((= char ?\()
  78.               (let ((parens 1))
  79.             (forward-char 1)
  80.             (while (and (not (eobp)) (not (zerop parens)))
  81.               (re-search-forward "[()]" nil 0)
  82.               (cond ((or (eobp)
  83.                      (= (char-after (- (point) 2)) ?\\)))
  84.                 ((= (preceding-char) ?\()
  85.                  (setq parens (1+ parens)))
  86.                 (t
  87.                  (setq parens (1- parens)))))))))
  88.          (setq s (buffer-substring start (point)))
  89.          (if (and (null (string-match "^[\t\f\n\r ]+$" s))
  90.               (not (string= s "")))
  91.          (setq list (cons s list)))
  92.          (nreverse list)) ; jwz: fixed order
  93.     (and work-buffer (kill-buffer work-buffer)))))))
  94.  
  95. (defun vm-parse-structured-header (string &optional sepchar keep-quotes)
  96.   (if (null string)
  97.       ()
  98.     (let ((work-buffer nil))
  99.       (save-excursion
  100.        (unwind-protect
  101.        (let ((list nil)
  102.          (nonspecials "^\"\\( \t\n\r\f")
  103.          start s char sp+sepchar)
  104.          (if sepchar
  105.          (setq nonspecials (concat nonspecials (list sepchar))
  106.                sp+sepchar (concat "\t\f\n\r " (list sepchar))))
  107.          (setq work-buffer (generate-new-buffer "*vm-work*"))
  108.          (buffer-disable-undo work-buffer)
  109.          (set-buffer work-buffer)
  110.          (insert string)
  111.          (goto-char (point-min))
  112.          (skip-chars-forward "\t\f\n\r ")
  113.          (setq start (point))
  114.          (while (not (eobp))
  115.            (skip-chars-forward nonspecials)
  116.            (setq char (following-char))
  117.            (cond ((looking-at "[ \t\n\r\f]")
  118.               (delete-char 1))
  119.              ((= char ?\\)
  120.               (forward-char 1)
  121.               (if (not (eobp))
  122.               (forward-char 1)))
  123.              ((and sepchar (= char sepchar))
  124.               (setq s (buffer-substring start (point)))
  125.               (if (or (null (string-match "^[\t\f\n\r ]+$" s))
  126.                   (not (string= s "")))
  127.               (setq list (cons s list)))
  128.               (skip-chars-forward sp+sepchar)
  129.               (setq start (point)))
  130.              ((looking-at " \t\n\r\f")
  131.               (skip-chars-forward " \t\n\r\f"))
  132.              ((= char ?\")
  133.               (let ((done nil))
  134.             (if keep-quotes
  135.                 (forward-char 1)
  136.               (delete-char 1))
  137.             (while (not done)
  138.               (if (null (re-search-forward "[\\\"]" nil t))
  139.                   (setq done t)
  140.                 (setq char (char-after (1- (point))))
  141.                 (cond ((char-equal char ?\\)
  142.                    (delete-char -1)
  143.                    (if (eobp)
  144.                        (setq done t)
  145.                      (forward-char 1)))
  146.                   (t (if (not keep-quotes)
  147.                      (delete-char -1))
  148.                      (setq done t)))))))
  149.              ((= char ?\()
  150.               (let ((done nil)
  151.                 (pos (point))
  152.                 (parens 1))
  153.             (forward-char 1)
  154.             (while (not done)
  155.               (if (null (re-search-forward "[\\()]" nil t))
  156.                   (setq done t)
  157.                 (setq char (char-after (1- (point))))
  158.                 (cond ((char-equal char ?\\)
  159.                    (if (eobp)
  160.                        (setq done t)
  161.                      (forward-char 1)))
  162.                   ((char-equal char ?\()
  163.                    (setq parens (1+ parens)))
  164.                   (t
  165.                    (setq parens (1- parens)
  166.                      done (zerop parens))))))
  167.             (delete-region pos (point))))))
  168.          (setq s (buffer-substring start (point)))
  169.          (if (and (null (string-match "^[\t\f\n\r ]+$" s))
  170.               (not (string= s "")))
  171.          (setq list (cons s list)))
  172.          (nreverse list))
  173.     (and work-buffer (kill-buffer work-buffer)))))))
  174.  
  175. (defun vm-write-string (where string)
  176.   (if (bufferp where)
  177.       (vm-save-buffer-excursion
  178.     (set-buffer where)
  179.     (goto-char (point-max))
  180.     (insert string))
  181.     (let ((temp-buffer nil)
  182.       (coding-system-for-read 'no-conversion)
  183.       (coding-system-for-write 'no-conversion))
  184.       (unwind-protect
  185.       (save-excursion
  186.         (setq temp-buffer (generate-new-buffer "*vm-work*"))
  187.         (set-buffer temp-buffer)
  188.         (insert string)
  189.         ;; correct for VM's uses of this function---
  190.         ;; writing out message separators
  191.         (setq buffer-file-type nil)
  192.         ;; Tell XEmacs/MULE to pick the correct newline conversion.
  193.         (and vm-xemacs-mule-p
  194.          (set-file-coding-system 'no-conversion nil))
  195.         (write-region (point-min) (point-max) where t 'quiet))
  196.     (and temp-buffer (kill-buffer temp-buffer))))))
  197.  
  198. (defmacro vm-marker (pos &optional buffer)
  199.   (list 'set-marker '(make-marker) pos buffer))
  200.  
  201. (defmacro vm-increment (variable)
  202.   (list 'setq variable (list '1+ variable)))
  203.  
  204. (defmacro vm-decrement (variable)
  205.   (list 'setq variable (list '1- variable)))
  206.  
  207. (defmacro vm-select-folder-buffer ()
  208.   '(and vm-mail-buffer
  209.     (or (buffer-name vm-mail-buffer)
  210.         (error "Folder buffer has been killed."))
  211.     (set-buffer vm-mail-buffer)))
  212.  
  213. (defun vm-check-for-killed-summary ()
  214.   (and (bufferp vm-summary-buffer) (null (buffer-name vm-summary-buffer))
  215.        (let ((mp vm-message-list))
  216.      (setq vm-summary-buffer nil)
  217.      (while mp
  218.        (vm-set-su-start-of (car mp) nil)
  219.        (vm-set-su-end-of (car mp) nil)
  220.        (setq mp (cdr mp))))))
  221.  
  222. (defun vm-check-for-killed-presentation ()
  223.   (and (bufferp vm-presentation-buffer-handle)
  224.        (null (buffer-name vm-presentation-buffer-handle))
  225.        (progn
  226.      (setq vm-presentation-buffer-handle nil
  227.            vm-presentation-buffer nil))))
  228.  
  229. (defun vm-check-for-killed-folder ()
  230.   (and (bufferp vm-mail-buffer) (null (buffer-name vm-mail-buffer))
  231.        (setq vm-mail-buffer nil)))
  232.  
  233. (defmacro vm-error-if-folder-read-only ()
  234.   '(while vm-folder-read-only
  235.      (signal 'folder-read-only (list (current-buffer)))))
  236.  
  237. (put 'folder-read-only 'error-conditions '(folder-read-only error))
  238. (put 'folder-read-only 'error-message "Folder is read-only")
  239.  
  240. (defmacro vm-error-if-virtual-folder ()
  241.   '(and (eq major-mode 'vm-virtual-mode)
  242.     (error "%s cannot be applied to virtual folders." this-command)))
  243.  
  244. (defmacro vm-build-threads-if-unbuilt ()
  245.   '(if (null vm-thread-obarray)
  246.        (vm-build-threads nil)))
  247.  
  248. (defun vm-abs (n) (if (< n 0) (- n) n))
  249.  
  250. ;; save-restriction flubs restoring the clipping region if you
  251. ;; (widen) and modify text outside the old region.
  252. ;; This should do it right.
  253. (defmacro vm-save-restriction (&rest forms)
  254.   (let ((vm-sr-clip (make-symbol "vm-sr-clip"))
  255.     (vm-sr-min (make-symbol "vm-sr-min"))
  256.     (vm-sr-max (make-symbol "vm-sr-max")))
  257.     (list 'let (list (list vm-sr-clip '(> (buffer-size)
  258.                       (- (point-max) (point-min))))
  259.              ;; this shouldn't be necessary but the
  260.              ;; byte-compiler turns these into interned symbols
  261.              ;; which utterly defeats the purpose of the
  262.              ;; make-symbol calls above.  Soooo, until the compiler
  263.              ;; is fixed, these must be made into (let ...)
  264.              ;; temporaries so that nested calls to this macros
  265.              ;; won't misbehave.
  266.              vm-sr-min vm-sr-max)
  267.       (list 'and vm-sr-clip
  268.         (list 'setq vm-sr-min '(set-marker (make-marker) (point-min)))
  269.         (list 'setq vm-sr-max '(set-marker (make-marker) (point-max))))
  270.       (list 'unwind-protect (cons 'progn forms)
  271.         '(widen)
  272.         (list 'and vm-sr-clip
  273.               (list 'progn
  274.                 (list 'narrow-to-region vm-sr-min vm-sr-max)
  275.                 (list 'set-marker vm-sr-min nil)
  276.                 (list 'set-marker vm-sr-max nil)))))))
  277.  
  278. (defmacro vm-save-buffer-excursion (&rest forms)
  279.   (list 'let '((vm-sbe-buffer (current-buffer)))
  280.     (list 'unwind-protect
  281.           (cons 'progn forms)
  282.           '(and (not (eq vm-sbe-buffer (current-buffer)))
  283.             (buffer-name vm-sbe-buffer)
  284.             (set-buffer vm-sbe-buffer)))))
  285.  
  286. (defun vm-last (list) (while (cdr-safe list) (setq list (cdr list))) list)
  287.  
  288. (defun vm-vector-to-list (vector)
  289.   (let ((i (1- (length vector)))
  290.     list)
  291.     (while (>= i 0)
  292.       (setq list (cons (aref vector i) list))
  293.       (vm-decrement i))
  294.     list ))
  295.  
  296. (defun vm-extend-vector (vector length &optional fill)
  297.   (let ((vlength (length vector)))
  298.     (if (< vlength length)
  299.     (apply 'vector (nconc (vm-vector-to-list vector)
  300.                   (make-list (- length vlength) fill)))
  301.       vector )))
  302.  
  303. (defun vm-obarray-to-string-list (blobarray)
  304.   (let ((list nil))
  305.     (mapatoms (function (lambda (s) (setq list (cons (symbol-name s) list))))
  306.           blobarray)
  307.     list ))
  308.  
  309. (defun vm-mapcar (function &rest lists)
  310.   (let (arglist result)
  311.     (while (car lists)
  312.       (setq arglist (mapcar 'car lists))
  313.       (setq result (cons (apply function arglist) result))
  314.       (setq lists (mapcar 'cdr lists)))
  315.     (nreverse result)))
  316.  
  317. (defun vm-mapc (function &rest lists)
  318.   (let (arglist)
  319.     (while (car lists)
  320.       (setq arglist (mapcar 'car lists))
  321.       (apply function arglist)
  322.       (setq lists (mapcar 'cdr lists)))))
  323.  
  324. (defun vm-delete (predicate list &optional reverse)
  325.   (let ((p list) (reverse (if reverse 'not 'identity)) prev)
  326.     (while p
  327.       (if (funcall reverse (funcall predicate (car p)))
  328.       (if (null prev)
  329.           (setq list (cdr list) p list)
  330.         (setcdr prev (cdr p))
  331.         (setq p (cdr p)))
  332.     (setq prev p p (cdr p))))
  333.     list ))
  334.  
  335. (defun vm-delete-directory-file-names (list)
  336.   (vm-delete 'file-directory-p list))
  337.  
  338. (defun vm-delete-backup-file-names (list)
  339.   (vm-delete 'backup-file-name-p list))
  340.  
  341. (defun vm-delete-auto-save-file-names (list)
  342.   (vm-delete 'auto-save-file-name-p list))
  343.  
  344. (defun vm-delete-index-file-names (list)
  345.   (vm-delete 'vm-index-file-name-p list))
  346.  
  347. (defun vm-index-file-name-p (file)
  348.   (and (file-regular-p file)
  349.        (stringp vm-index-file-suffix)
  350.        (let ((str (concat (regexp-quote vm-index-file-suffix) "$")))
  351.      (string-match str file))
  352.        t ))
  353.  
  354. (defun vm-delete-duplicates (list &optional all hack-addresses)
  355.   "Delete duplicate equivalent strings from the list.
  356. If ALL is t, then if there is more than one occurrence of a string in the list,
  357.  then all occurrences of it are removed instead of just the subsequent ones.
  358. If HACK-ADDRESSES is t, then the strings are considered to be mail addresses,
  359.  and only the address part is compared (so that \"Name <foo>\" and \"foo\"
  360.  would be considered to be equivalent.)"
  361.   (let ((hashtable vm-delete-duplicates-obarray)
  362.     (new-list nil)
  363.     sym-string sym)
  364.     (fillarray hashtable 0)
  365.     (while list
  366.       (setq sym-string
  367.         (if hack-addresses
  368.         (nth 1 (funcall vm-chop-full-name-function (car list)))
  369.           (car list))
  370.         sym-string (or sym-string "-unparseable-garbage-")
  371.         sym (intern sym-string hashtable))
  372.       (if (boundp sym)
  373.       (and all (setcar (symbol-value sym) nil))
  374.     (setq new-list (cons (car list) new-list))
  375.     (set sym new-list))
  376.       (setq list (cdr list)))
  377.     (delq nil (nreverse new-list))))
  378.  
  379. (defun vm-member-0 (thing list)
  380.   (catch 'done
  381.     (while list
  382.       (and (equal (car list) thing)
  383.        (throw 'done list))
  384.       (setq list (cdr list)))
  385.     nil ))
  386.  
  387. (fset 'vm-member (symbol-function (if (fboundp 'member) 'member 'vm-member-0)))
  388.  
  389. (defun vm-delqual (ob list)
  390.   (let ((prev nil)
  391.     (curr list))
  392.     (while curr
  393.       (if (not (equal ob (car curr)))
  394.       (setq prev curr
  395.         curr (cdr curr))
  396.     (if (null prev)
  397.         (setq list (cdr list)
  398.           curr list)
  399.       (setq curr (cdr curr))
  400.       (setcdr prev curr))))
  401.     list ))
  402.  
  403. (defun vm-copy-local-variables (buffer &rest variables)
  404.   (let ((values (mapcar 'symbol-value variables)))
  405.     (save-excursion
  406.       (set-buffer buffer)
  407.       (vm-mapc 'set variables values))))
  408.  
  409. (put 'folder-empty 'error-conditions '(folder-empty error))
  410. (put 'folder-empty 'error-message "Folder is empty")
  411. (put 'unrecognized-folder-type 'error-conditions
  412.      '(unrecognized-folder-type error))
  413. (put 'unrecognized-folder-type 'error-message "Unrecognized folder type")
  414.  
  415. (defun vm-error-if-folder-empty ()
  416.   (while (null vm-message-list)
  417.     (if vm-folder-type
  418.     (signal 'unrecognized-folder-type nil)
  419.       (signal 'folder-empty nil))))
  420.  
  421. (defun vm-copy (object)
  422.   (cond ((consp object)
  423.      (let (return-value cons)
  424.        (setq return-value (cons (vm-copy (car object)) nil)
  425.          cons return-value
  426.          object (cdr object))
  427.        (while (consp object)
  428.          (setcdr cons (cons (vm-copy (car object)) nil))
  429.          (setq cons (cdr cons)
  430.            object (cdr object)))
  431.        (setcdr cons object)
  432.        return-value ))
  433.     ((vectorp object) (apply 'vector (mapcar 'vm-copy object)))
  434.     ((stringp object) (copy-sequence object))
  435.     ((markerp object) (copy-marker object))
  436.     (t object)))
  437.  
  438. (defun vm-multiple-frames-possible-p () 
  439.   (cond (vm-xemacs-p 
  440.      (or (memq 'win (device-matching-specifier-tag-list))
  441.          (featurep 'tty-frames)))
  442.         (vm-fsfemacs-p 
  443.          (fboundp 'make-frame))))
  444.  
  445. (defun vm-mouse-support-possible-p () 
  446.   (cond (vm-xemacs-p 
  447.          (featurep 'window-system)) 
  448.         (vm-fsfemacs-p 
  449.          (fboundp 'track-mouse))))
  450.  
  451. (defun vm-mouse-support-possible-here-p ()
  452.   (cond (vm-xemacs-p
  453.      (memq 'win (device-matching-specifier-tag-list)))
  454.     (vm-fsfemacs-p
  455.      (eq window-system 'x))))
  456.  
  457. (defun vm-menu-support-possible-p ()
  458.   (cond (vm-xemacs-p
  459.      (featurep 'menubar))
  460.     (vm-fsfemacs-p
  461.      (fboundp 'menu-bar-mode))))
  462.  
  463. (defun vm-toolbar-support-possible-p ()
  464.   (and vm-xemacs-p (featurep 'toolbar)))
  465.  
  466. (defun vm-multiple-fonts-possible-p ()
  467.   (cond (vm-xemacs-p
  468.      (eq (device-type) 'x))
  469.     (vm-fsfemacs-p
  470.      (or (eq window-system 'x)
  471.          (eq window-system 'win32)))))
  472.  
  473. (defun vm-run-message-hook (message &optional hook-variable)
  474.   (save-excursion
  475.     (set-buffer (vm-buffer-of message))
  476.     (vm-save-restriction
  477.       (widen)
  478.       (save-excursion
  479.     (narrow-to-region (vm-headers-of message) (vm-text-end-of message))
  480.     (run-hooks hook-variable)))))
  481.  
  482. (defun vm-error-free-call (function &rest args)
  483.   (condition-case nil
  484.       (apply function args)
  485.     (error nil)))
  486.  
  487. (put 'beginning-of-folder 'error-conditions '(beginning-of-folder error))
  488. (put 'beginning-of-folder 'error-message "Beginning of folder")
  489. (put 'end-of-folder 'error-conditions '(end-of-folder error))
  490. (put 'end-of-folder 'error-message "End of folder")
  491.  
  492. (defun vm-trace (&rest args)
  493.   (save-excursion
  494.     (set-buffer (get-buffer-create "*vm-trace*"))
  495.     (apply 'insert args)))
  496.  
  497. (defun vm-timezone-make-date-sortable (string)
  498.   (or (cdr (assq string vm-sortable-date-alist))
  499.       (let ((vect (vm-parse-date string))
  500.         (date (vm-parse (current-time-string) " *\\([^ ]+\\)")))
  501.     ;; if specified date is incomplete fill in the holes
  502.     ;; with useful information, defaulting to the current
  503.     ;; date and timezone for everything except hh:mm:ss which
  504.     ;; defaults to midnight.
  505.     (if (equal (aref vect 1) "")
  506.         (aset vect 1 (nth 2 date)))
  507.     (if (equal (aref vect 2) "")
  508.         (aset vect 2 (nth 1 date)))
  509.     (if (equal (aref vect 3) "")
  510.         (aset vect 3 (nth 4 date)))
  511.     (if (equal (aref vect 4) "")
  512.         (aset vect 4 "00:00:00"))
  513.     (if (equal (aref vect 5) "")
  514.         (aset vect 5 (vm-current-time-zone)))
  515.     ;; save this work so we won't have to do it again
  516.     (setq vm-sortable-date-alist
  517.           (cons (cons string
  518.               (condition-case nil
  519.                   (timezone-make-date-sortable
  520.                    (format "%s %s %s %s %s"
  521.                        (aref vect 1)
  522.                        (aref vect 2)
  523.                        (aref vect 3)
  524.                        (aref vect 4)
  525.                        (aref vect 5)))
  526.                 (error "1970010100:00:00")))
  527.             vm-sortable-date-alist))
  528.     ;; return result
  529.     (cdr (car vm-sortable-date-alist)))))
  530.  
  531. (defun vm-current-time-zone ()
  532.   (or (condition-case nil
  533.       (let* ((zone (car (current-time-zone)))
  534.          (absmin (/ (vm-abs zone) 60)))
  535.         (format "%c%02d%02d" (if (< zone 0) ?- ?+)
  536.             (/ absmin 60) (% absmin 60)))
  537.     (error nil))
  538.       (let ((temp-buffer nil))
  539.     (condition-case nil
  540.         (unwind-protect
  541.         (save-excursion
  542.           (setq temp-buffer (generate-new-buffer "*vm-work*"))
  543.           (set-buffer temp-buffer)
  544.           (call-process "date" nil temp-buffer nil)
  545.           (nth 4 (vm-parse (vm-buffer-string-no-properties)
  546.                    " *\\([^ ]+\\)")))
  547.           (and temp-buffer (kill-buffer temp-buffer)))
  548.       (error nil)))
  549.       ""))
  550.  
  551. (defun vm-should-generate-summary ()
  552.   (cond ((eq vm-startup-with-summary t) t)
  553.     ((integerp vm-startup-with-summary)
  554.      (let ((n vm-startup-with-summary))
  555.        (cond ((< n 0) (null (nth (vm-abs n) vm-message-list)))
  556.          (t (nth (1- n) vm-message-list)))))
  557.     (vm-startup-with-summary t)
  558.     (t nil)))
  559.  
  560. (defun vm-find-composition-buffer (&optional not-picky)
  561.   (let ((b-list (buffer-list)) choice alternate)
  562.     (save-excursion
  563.      (while b-list
  564.        (set-buffer (car b-list))
  565.        (if (eq major-mode 'mail-mode)
  566.        (if (buffer-modified-p)
  567.            (setq choice (current-buffer)
  568.              b-list nil)
  569.          (and not-picky (null alternate)
  570.           (setq alternate (current-buffer)))
  571.          (setq b-list (cdr b-list)))
  572.      (setq b-list (cdr b-list))))
  573.     (or choice alternate))))
  574.  
  575. (defun vm-get-file-buffer (file)
  576.   "Like get-file-buffer, but also checks buffers against FILE's truename"
  577.   (or (get-file-buffer file)
  578.       (and (fboundp 'file-truename)
  579.        (get-file-buffer (file-truename file)))))
  580.  
  581. (defun vm-set-region-face (start end face)
  582.   (let ((e (vm-make-extent start end)))
  583.     (vm-set-extent-property e 'face face)))
  584.  
  585. (defun vm-default-buffer-substring-no-properties (beg end &optional buffer)
  586.   (let ((s (if buffer
  587.            (save-excursion
  588.          (set-buffer buffer)
  589.          (buffer-substring beg end))
  590.          (buffer-substring beg end))))
  591.     (set-text-properties 0 (length s) nil s)
  592.     (copy-sequence s)))
  593.  
  594. (fset 'vm-buffer-substring-no-properties
  595.   (cond ((fboundp 'buffer-substring-no-properties)
  596.      (function buffer-substring-no-properties))
  597.     (vm-xemacs-p
  598.      (function buffer-substring))
  599.     (t (function vm-default-buffer-substring-no-properties))))
  600.  
  601. (defun vm-buffer-string-no-properties ()
  602.   (vm-buffer-substring-no-properties (point-min) (point-max)))
  603.  
  604. (defun vm-insert-region-from-buffer (buffer &optional start end)
  605.   (let ((target-buffer (current-buffer)))
  606.     (set-buffer buffer)
  607.     (save-restriction
  608.       (widen)
  609.       (or start (setq start (point-min)))
  610.       (or end (setq end (point-max)))
  611.       (set-buffer target-buffer)
  612.       (insert-buffer-substring buffer start end)
  613.       (set-buffer buffer))
  614.     (set-buffer target-buffer)))
  615.  
  616. (if (not (fboundp 'vm-extent-property))
  617.     (if (fboundp 'overlay-get)
  618.     (fset 'vm-extent-property 'overlay-get)
  619.       (fset 'vm-extent-property 'extent-property)))
  620.  
  621. (if (not (fboundp 'vm-set-extent-property))
  622.     (if (fboundp 'overlay-put)
  623.     (fset 'vm-set-extent-property 'overlay-put)
  624.       (fset 'vm-set-extent-property 'set-extent-property)))
  625.  
  626. (if (not (fboundp 'vm-set-extent-endpoints))
  627.     (if (fboundp 'move-overlay)
  628.     (fset 'vm-set-extent-endpoints 'move-overlay)
  629.       (fset 'vm-set-extent-endpoints 'set-extent-endpoints)))
  630.  
  631. (if (not (fboundp 'vm-make-extent))
  632.     (if (fboundp 'make-overlay)
  633.     (fset 'vm-make-extent 'make-overlay)
  634.       (fset 'vm-make-extent 'make-extent)))
  635.  
  636. (if (not (fboundp 'vm-extent-end-position))
  637.     (if (fboundp 'overlay-end)
  638.     (fset 'vm-extent-end-position 'overlay-end)
  639.       (fset 'vm-extent-end-position 'extent-end-position)))
  640.  
  641. (if (not (fboundp 'vm-extent-start-position))
  642.     (if (fboundp 'overlay-start)
  643.     (fset 'vm-extent-start-position 'overlay-start)
  644.       (fset 'vm-extent-start-position 'extent-start-position)))
  645.  
  646. (if (not (fboundp 'vm-detach-extent))
  647.     (if (fboundp 'delete-overlay)
  648.     (fset 'vm-detach-extent 'delete-overlay)
  649.       (fset 'vm-detach-extent 'detach-extent)))
  650.  
  651. (if (not (fboundp 'vm-extent-properties))
  652.     (if (fboundp 'overlay-properties)
  653.     (fset 'vm-extent-properties 'overlay-properties)
  654.       (fset 'vm-extent-properties 'extent-properties)))
  655.  
  656. (defun vm-copy-extent (e)
  657.   (let ((props (vm-extent-properties e))
  658.     (ee (vm-make-extent (vm-extent-start-position e)
  659.                 (vm-extent-end-position e))))
  660.     (while props
  661.       (vm-set-extent-property ee (car props) (car (cdr props)))
  662.       (setq props (cdr (cdr props))))))
  663.  
  664. (defun vm-make-tempfile-name ()
  665.   (let ((done nil) (pid (emacs-pid)) filename)
  666.     (while (not done)
  667.       (setq filename (expand-file-name (format "vm%d.%d" pid
  668.                            vm-tempfile-counter)
  669.                        vm-temp-file-directory)
  670.         vm-tempfile-counter (1+ vm-tempfile-counter)
  671.         done (not (file-exists-p filename))))
  672.     filename ))
  673.  
  674. (defun vm-make-work-buffer (&optional name)
  675.   (let (work-buffer)
  676.     (setq work-buffer (generate-new-buffer (or name "*vm-workbuf*")))
  677.     (buffer-disable-undo work-buffer)
  678.     work-buffer ))
  679.  
  680. (defun vm-insert-char (char &optional count ignored buffer)
  681.   (condition-case nil
  682.       (progn
  683.     (insert-char char count ignored buffer)
  684.     (fset 'vm-insert-char 'insert-char))
  685.     (wrong-number-of-arguments
  686.      (fset 'vm-insert-char 'vm-xemacs-compatible-insert-char)
  687.      (vm-insert-char char count ignored buffer))))
  688.  
  689. (defun vm-xemacs-compatible-insert-char (char &optional count ignored buffer)
  690.   (if (and buffer (eq buffer (current-buffer)))
  691.       (insert-char char count)
  692.     (save-excursion
  693.       (set-buffer buffer)
  694.       (insert-char char count))))
  695.  
  696. (defun vm-symbol-lists-intersect-p (list1 list2)
  697.   (catch 'done
  698.     (while list1
  699.       (and (memq (car list1) list2)
  700.        (throw 'done t))
  701.       (setq list1 (cdr list1)))
  702.     nil ))
  703.  
  704. (defun vm-set-buffer-variable (buffer var value)
  705.   (save-excursion
  706.     (set-buffer buffer)
  707.     (set var value)))
  708.  
  709. (defun vm-buffer-variable-value (buffer var)
  710.   (save-excursion
  711.     (set-buffer buffer)
  712.     (symbol-value var)))
  713.  
  714. (defsubst vm-with-string-as-temp-buffer (string function)
  715.   (let ((work-buffer nil))
  716.     (unwind-protect
  717.     (save-excursion
  718.       (setq work-buffer (generate-new-buffer " *work*"))
  719.       (set-buffer work-buffer)
  720.       (insert string)
  721.       (funcall function)
  722.       (buffer-string))
  723.       (and work-buffer (kill-buffer work-buffer)))))
  724.  
  725. (defun vm-string-assoc (elt list)
  726.   (let ((case-fold-search t)
  727.     (found nil)
  728.     (elt (regexp-quote elt)))
  729.     (while (and list (not found))
  730.       (if (and (equal 0 (string-match elt (car (car list))))
  731.            (= (match-end 0) (length (car (car list)))))
  732.       (setq found t)
  733.     (setq list (cdr list))))
  734.     (car list)))
  735.  
  736. (defun vm-string-member (elt list)
  737.   (let ((case-fold-search t)
  738.     (found nil)
  739.     (elt (regexp-quote elt)))
  740.     (while (and list (not found))
  741.       (if (and (equal 0 (string-match elt (car list)))
  742.            (= (match-end 0) (length (car list))))
  743.       (setq found t)
  744.     (setq list (cdr list))))
  745.     list))
  746.  
  747. (defmacro vm-assert (expression)
  748.   (list 'or expression
  749.     (list 'progn
  750.           (list 'setq 'debug-on-error t)
  751.           (list 'error "assertion failed: %S"
  752.             (list 'quote expression)))))
  753.  
  754. (defun vm-time-difference (t1 t2)
  755.   (let (usecs secs 65536-secs carry)
  756.     (setq usecs (- (nth 2 t1) (nth 2 t2)))
  757.     (if (< usecs 0)
  758.     (setq carry 1
  759.           usecs (+ usecs 1000000))
  760.       (setq carry 0))
  761.     (setq secs (- (nth 1 t1) (nth 1 t2) carry))
  762.     (if (< secs 0)
  763.      (setq carry 1
  764.            secs (+ secs 65536))
  765.       (setq carry 0))
  766.     (setq 65536-secs (- (nth 0 t1) (nth 0 t2) carry))
  767.     (+ (* 65536-secs 65536)
  768.        secs
  769.        (/ usecs (if (featurep 'lisp-float-type) 1e6 1000000)))))
  770.  
  771. (if (fboundp 'char-to-int)
  772.     (fset 'vm-char-to-int 'char-to-int)
  773.   (fset 'vm-char-to-int 'identity))
  774.